package com.exam.system.controller;

import com.exam.system.entity.ExamPaper;
import com.exam.system.entity.PageDTO;
import com.exam.system.entity.ResponseEntity;
import com.exam.system.service.ExamPaperService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExamPaperController
 * @Description
 * @Author ckl
 * @DATE 2021/5/31 17:53
 * @versions 1.0
 */
@Api(tags = "试卷模块")
@RestController
@CrossOrigin
@Slf4j
public class ExamPaperController {
    @Autowired
    private ExamPaperService examPaperService;

    /**
     * 添加试卷
     *
     * @param examPaper 添加试卷
     */
    @ApiImplicitParam(name = "paperPageDTO", value = "分页工具类类", required = false)
    @ApiOperation(value = "添加试卷，和修改试卷共用一个方法")
    @PostMapping("paper/addPaper")
    public void addPaper(@RequestBody Map<String, String> maps,ServletRequest request) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ExamPaper examPaper = objectMapper.readValue(maps.get("examPaper"), new TypeReference<ExamPaper>() {});
        List<Integer> quesIdList = objectMapper.readValue(maps.get("quesIdList"), new TypeReference<List<Integer>>() {});
        examPaperService.addPaper(examPaper, quesIdList,request);
    }

    /**
     * 多条件分页查询
     *
     * @param paperPageDTO 分页中间表对象
     * @return 返回分页后的对象
     */
    @ApiImplicitParam(name = "paperPageDTO", value = "分页工具类类", required = false)
    @ApiOperation(value = "试卷列表的多条件分页查询")
    @PostMapping("paper/findAllPaper")
    public ResponseEntity<PageInfo<Map<String, String>>> findAllPaper(@RequestBody PageDTO<ExamPaper> paperPageDTO) {
        return new ResponseEntity<>(200, "success", examPaperService.findAllPaper(paperPageDTO));
    }

    /**
     * 删除试卷
     * @param id
     * @return
     */
    @ApiImplicitParam(name = "id",value = "用于删除试卷",required = true)
    @ApiOperation(value="根据试卷id删除对应的试卷")
    @RequestMapping("paper/delPaperById/{id}")
    public ResponseEntity<String> delPaperById(@PathVariable Integer id){
        examPaperService.delPaperById(id);
        return new ResponseEntity<>(200, "删除试卷成功", null);
    }
}


